//
//  ChooseView.m
//  CarUser
//
//  Created by 王森林 on 2019/3/4.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "ChooseView.h"


@interface ChooseView()
@property  (nonatomic, copy) void (^resultBlock)(id sales);
@property (nonatomic,  strong) NSArray *ary;
@property (nonatomic,assign) NSInteger selectIndex;
@end

@implementation ChooseView

- (instancetype) initWithFrame:(CGRect)frame withData:(NSArray *)dataAry withIndex:(NSInteger)index  withBlock:(void(^)(id sales))result
{
    self = [super initWithFrame:frame];
    if (self) {
        self.resultBlock = result;
        self.ary = [NSArray arrayWithArray:dataAry];
        self.selectIndex = index;
//        [self createUI];
    }
    return self;
    
}

- (void)createUI
{
    [self.ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn  = [UIButton  buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 45*idx, BOUNDWIDTH, 45);
        btn.tag = idx;
        [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        UILabel  *line = [[UILabel alloc] initWithFrame:CGRectMake(15, 44.5, BOUNDWIDTH - 30, 0.5)];
        line.backgroundColor  = Common_SepeColor;
        [btn  addSubview:line];
        
        UILabel *titleLabel = [[UILabel  alloc]  initWithFrame:CGRectMake(15, 0, 120, 44)];
        titleLabel.textColor =  Common_TextColor;
        titleLabel.font  = FONT_SIZE(13);
        titleLabel.text = obj;
        [btn addSubview:titleLabel];
        
        UIImageView  *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 15, 18, 14)];
        imageView.image = IMG(@"choose_select");
        imageView.hidden  = YES;
        [btn addSubview:imageView];
        
        
        
        if (self.selectIndex == idx) {
            imageView.hidden = NO;
            titleLabel.textColor = [UIColor redColor];
        }
        
//        UIImage  *imageView
        
    }];
}


- (void)handleBtnAction:(id)sender
{
    UIButton *btn =  sender;
    if (self.resultBlock) {
        self.resultBlock([NSString stringWithFormat:@"%ld",(btn.tag+1)]);
    }
    
}

@end
